package com.roadjava.demo05.util;

import com.roadjava.demo05.bean.UserInfo5;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

public class ValidationUtil5 {
    private static Validator validator;

    static {
        validator = Validation.buildDefaultValidatorFactory().getValidator();
    }

    public static List<String> valid(UserInfo5 userInfo, Class<?>... groups) {
        Set<ConstraintViolation<UserInfo5>> set = validator.validate(userInfo, groups);
        List<String> list = set.stream()
                .map(v ->
                        "属性：" + v.getPropertyPath()
                                + "，属性的值：" + v.getInvalidValue()
                                + "，校验不通过的提示信息：" + v.getMessage())
                .collect(Collectors.toList());
        return list;
    }
}